home *** CD-ROM | disk | FTP | other *** search
/ NOVA - For the NeXT Workstation / NOVA - For the NeXT Workstation.iso / SourceCode / AdobeExamples / NX_Binary / DrawingView.m < prev    next >
Encoding:
Text File  |  1992-12-19  |  6.5 KB  |  279 lines

  1.  
  2. /*
  3.  * (a)  (C) 1990 by Adobe Systems Incorporated. All rights reserved.
  4.  *
  5.  * (b)  If this Sample Code is distributed as part of the Display PostScript
  6.  *    System Software Development Kit from Adobe Systems Incorporated,
  7.  *    then this copy is designated as Development Software and its use is
  8.  *    subject to the terms of the License Agreement attached to such Kit.
  9.  *
  10.  * (c)  If this Sample Code is distributed independently, then the following
  11.  *    terms apply:
  12.  *
  13.  * (d)  This file may be freely copied and redistributed as long as:
  14.  *    1) Parts (a), (d), (e) and (f) continue to be included in the file,
  15.  *    2) If the file has been modified in any way, a notice of such
  16.  *      modification is conspicuously indicated.
  17.  *
  18.  * (e)  PostScript, Display PostScript, and Adobe are registered trademarks of
  19.  *    Adobe Systems Incorporated.
  20.  * 
  21.  * (f) THE INFORMATION BELOW IS FURNISHED AS IS, IS SUBJECT TO
  22.  *    CHANGE WITHOUT NOTICE, AND SHOULD NOT BE CONSTRUED
  23.  *    AS A COMMITMENT BY ADOBE SYSTEMS INCORPORATED.
  24.  *    ADOBE SYSTEMS INCORPORATED ASSUMES NO RESPONSIBILITY
  25.  *    OR LIABILITY FOR ANY ERRORS OR INACCURACIES, MAKES NO
  26.  *    WARRANTY OF ANY KIND (EXPRESS, IMPLIED OR STATUTORY)
  27.  *    WITH RESPECT TO THIS INFORMATION, AND EXPRESSLY
  28.  *    DISCLAIMS ANY AND ALL WARRANTIES OF MERCHANTABILITY, 
  29.  *    FITNESS FOR PARTICULAR PURPOSES AND NONINFRINGEMENT
  30.  *    OF THIRD PARTY RIGHTS.
  31.  */
  32.  
  33. /*
  34.  *    DrawingView.m
  35.  *
  36.  *    This view represents the page that the image is drawn onto. It is
  37.  *    a subview of the DocView. The DocView is the document view of
  38.  *    the ClipView. This view's real size grows with the scale but the
  39.  *    bounds always stays the same.
  40.  *
  41.  *    Version:    2.0
  42.  *    Author:    Ken Fromm
  43.  *    History:
  44.  *            03-07-91        Added this comment.
  45.  */
  46.  
  47. #import "DrawingView.h"
  48. #import "DrawingViewWraps.h"
  49. #import "DocView.h"
  50. #import "Graphic.h"
  51.  
  52. #import <appkit/Button.h>
  53. #import <appkit/Control.h>
  54. #import <appkit/Matrix.h>
  55. #import <appkit/SavePanel.h>
  56. #import <appkit/nextstd.h>
  57.  
  58. #import <objc/List.h>
  59.  
  60. #import <dpsclient/dpsclient.h>
  61. #import <dpsclient/wraps.h>
  62.  
  63. extern void initGparms();
  64. extern void setGraphicState();
  65.  
  66. static const DPSNameEncoding  nameEncoding[4] = {dps_strings, dps_strings,
  67.     dps_indexed, dps_indexed};
  68. static const DPSProgramEncoding  programEncoding[4] = {dps_ascii, dps_ascii,
  69.     dps_binObjSeq, dps_encodedTokens};
  70. static const char  *fileSuffix[4] = {"Asc", "Abbr", "Bos", "Bot"};
  71.  
  72. @implementation DrawingView
  73.  
  74. /*
  75. *    Allocate a gstate, set the clipping to NO because it will be clipped
  76. *    by the clip view.
  77. */
  78. +newFrame:(NXRect *) frm
  79. {    
  80.     self = [super newFrame:frm]; 
  81.     [[self allocateGState] setClipping:NO]; 
  82.     
  83.     drawUpath = [NXApp  getUpathBuffer];
  84.  
  85.     PSWDefs();
  86.  
  87.     format = BOS;
  88.  
  89.     return self;
  90. }
  91.  
  92. - free
  93. {
  94.     [self  freeGraphics];
  95.  
  96.     return [super free];
  97. }
  98.  
  99. - freeGraphics
  100. {
  101.     if (graphicsListId)
  102.         [graphicsListId  freeObjects];
  103.         
  104.     [graphicsListId free];
  105.  
  106.     return self;
  107. }
  108.  
  109. - insertList:listId
  110. {
  111.     [self  freeGraphics];
  112.     graphicsListId = listId;
  113.  
  114.     return self;
  115. }
  116.  
  117. - setDrawOrigin:(NXPoint *)origin
  118. {
  119.     drawOrigin = *origin;
  120.     
  121.     return self;
  122. }
  123.  
  124. /*
  125.  *    Saves the file in encapsulated format.  A SavePanel is put up to
  126.  *    ask the user what file name should be used. A suffix is added to
  127.  *    identify the format that has been saved.
  128.  */
  129. - saveFile:sender
  130. {
  131.     id        savepanel, formatmatrix;
  132.  
  133.     int        count;
  134.  
  135.     char        tempBuf[MAXPATHLEN+1];
  136.  
  137.     char        *fileend;
  138.  
  139.     NXStream    *stream;
  140.  
  141.     DPSContext    newContext, oldContext;
  142.  
  143.     savepanel = [SavePanel new];
  144.     formatmatrix = [NXApp  formatMatrix];
  145.  
  146.     getwd(tempBuf);
  147.     if ([savepanel  runModalForDirectory:tempBuf  file:NULL])
  148.     {
  149.         strcpy(tempBuf, [savepanel filename]);
  150.         fileend = strrchr(tempBuf, '.');
  151.         if (!fileend)
  152.             fileend = &tempBuf[strlen(tempBuf)];
  153.  
  154.         oldContext = DPSGetCurrentContext();
  155.         count = [formatmatrix  cellCount];
  156.         for (format = 0; format < count; format++)
  157.         {
  158.             if ([[formatmatrix  cellAt:format :0]  state])
  159.             {
  160.                 stream = NXOpenMemory(NULL, 0, NX_WRITEONLY);
  161.                 if (stream)
  162.                 {                    
  163.                     newContext = DPSCreateStreamContext(stream, 0,
  164.                         programEncoding[format], nameEncoding[format], NULL);
  165.                     DPSSetContext(newContext);
  166.  
  167.                     [self  printPrologue:tempBuf];
  168.                     [self  drawSelf:&bounds  :1];
  169.                     [self  printTrailer];
  170.  
  171.                     strcpy(fileend, fileSuffix[format]);
  172.                     strcpy(&tempBuf[strlen(tempBuf)], ".eps");
  173.                     NXSaveToFile(stream, tempBuf);
  174.                     NXCloseMemory(stream, NX_FREEBUFFER);
  175.  
  176.                     DPSSetContext(oldContext);
  177.                     if (newContext != oldContext);
  178.                         DPSDestroyContext(newContext);
  179.                 }
  180.             }
  181.         }
  182.         format = BOS;
  183.     }
  184.  
  185.     return self;
  186. }
  187.  
  188. - printPrologue:(char *) filename
  189. {
  190.     time_t        clock;
  191.  
  192.     char            *printtime;
  193.  
  194.     DPSContext    ctxt;
  195.     
  196.     ctxt = DPSGetCurrentContext();
  197.     clock = time(0);
  198.     printtime = ctime(&clock);
  199.  
  200.     DPSPrintf(ctxt, "%%!PS-Adobe-2.0 EPSF-1.2\n");
  201.     DPSPrintf(ctxt, "%%%%Creator: %s\n", [NXApp  appName]);
  202.     DPSPrintf(ctxt, "%%%%CreationDate: %s", printtime);
  203.     DPSPrintf(ctxt, "%%%%Title: %s\n", filename);
  204.     DPSPrintf(ctxt, "%%%%Origin: %f %f\n", bounds.origin.x, bounds.origin.y);
  205.     DPSPrintf(ctxt, "%%%%BoundingBox: %f %f %f %f\n", bounds.origin.x, bounds.origin.y,
  206.         bounds.origin.x + bounds.size.width, bounds.origin.y + bounds.size.height);
  207.     if (format == ABBR)
  208.         DPSPrintf(ctxt, "%%%%DocumentProcSets: Abbreviations 1.0  0\n");
  209.     DPSPrintf(ctxt, "%%%%EndComments\n\n");
  210.  
  211.     if (format == ABBR)
  212.     {
  213.         DPSPrintf(ctxt, "%%%%BeginProcSet: Abbreviations 1.0 0\n");
  214.         PSWDefs();
  215.         DPSPrintf(ctxt, "%%%%EndProcSet\n");
  216.     }
  217.     else if (format == BOS || format == BOT)
  218.         DPSPrintf(ctxt, "/defineusername {pop pop} bind def\n");
  219.  
  220.     DPSPrintf(ctxt, "\n%%%%EndProlog\n\n");
  221.     DPSPrintf(ctxt, "realtime\n\n");
  222.  
  223.     return self;
  224. }
  225.  
  226. - printTrailer
  227. {
  228.     DPSContext    ctxt;
  229.     
  230.     ctxt = DPSGetCurrentContext();
  231.     DPSPrintf(ctxt, "realtime exch sub == flush\n");
  232.     DPSFlushContext(ctxt);
  233.  
  234.     return self;
  235. }    
  236.  
  237. /*
  238. *    Compare the bounds of the object with the rectangle to draw in order to
  239. *    eliminate unnecessary drawing. The modal session stuff is just to
  240. *    intercept the selection of the redraw button in the interface during
  241. *    a trace. (This stops the drawing. It's only check during a trace
  242. *    because the trace can be quite long.)
  243. */
  244. - drawSelf:(NXRect *)r :(int) count
  245. {
  246.     int            i, num;
  247.  
  248.     GParms        parms;
  249.     GParms        *p_parms;
  250.     
  251.     NXRect        rect;
  252.     NXRect        *p_rect;
  253.  
  254.     PSsetgray(NX_WHITE);
  255.     NXRectFill(r);
  256.  
  257.     initGparms(&parms);
  258.     p_parms = &parms;
  259.     setGraphicState(NULL, &parms, (format == ABBR));
  260.     
  261.     rect = *r;
  262.     rect.origin.x += drawOrigin.x;
  263.     rect.origin.y += drawOrigin.y;
  264.     p_rect = ▭
  265.     
  266.     PSgsave();
  267.         NXRectClip(r);
  268.         PStranslate(-drawOrigin.x, -drawOrigin.y);
  269.         num = [graphicsListId count];
  270.         for (i = 0; i < num; i++)
  271.             [[graphicsListId  objectAt:i]  drawObject:p_rect  currentParms:p_parms
  272.                 withFormat:format];
  273.     PSgrestore();
  274.  
  275.     return self;            
  276. }
  277.  
  278. @end
  279.